function cargarAutoCompleteUbigeo() {
    var url = URL + 'MaePersonas/Ubigeo';
    //alert(url);
    try {
        $.get(url, function (data) {

            data = data.replace(/(\r\n|\n|\r)/gm, "");
            data = data.substr(0, data.length - 3) + "];";
            var div = document.getElementById("divUbigeo");
            if (div) {
                $("#divUbigeo").html(data);
                data = $("#divUbigeo").text();
            }
            //alert(data);
            eval(data);
            //$("#depPadre").dataSrc = "dataDependencia";
            //eval(data);
            cargarDepartamento();
        });
    } catch (e) { alert(e.Message); }
}

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

function cargarDepartamento() {
    var selectDepto = document.getElementById("PersonaDireccion_sUbigeoDepartamento");
    var newArrayUbigeoDepto = new Array();
    
    for (index in dataUbigeo) {
        longUbigeo = dataUbigeo[index].length;
        if (longUbigeo == 2) {
            newIndex = parseInt(dataUbigeo[index] * 1);
            newArrayUbigeoDepto[newIndex] = dataUbigeoDesc[index];
            //selectDepto.options[selectDepto.options.length] = new Option(dataUbigeo[index] + '-' + dataUbigeoDesc[index], index);
        }
    }
    selectDepto.options[0] = new Option("--Seleccione--","");
    for (i = 1; i < newArrayUbigeoDepto.length; i++) {
        selectDepto.options[selectDepto.options.length] = new Option(newArrayUbigeoDepto[i], pad(i, 2));
    }
}

function cargarProvincias(ddlDepartamento) {
    idDpto = ddlDepartamento.value;
    var selectProv = document.getElementById("PersonaDireccion_sUbigeoProvincia");
    selectProv.innerHTML = "";
    for (index in dataUbigeo) {
        substrUbigeo = dataUbigeo[index].substring(0, 2);
        longUbigeo = dataUbigeo[index].length;
        if (substrUbigeo == idDpto) {
            if (longUbigeo == 4) {
                selectProv.options[selectProv.options.length] = new Option(dataUbigeoDesc[index], dataUbigeo[index]);
            }
        }

    }
    cargarDistritos(selectProv);
}

function cargarDistritos(ddlProvincia) {
    idProv = ddlProvincia.value;
    var selectDistrito = document.getElementById("PersonaDireccion_sUbigeoDistrito");
    selectDistrito.innerHTML = "";
    for (index in dataUbigeo) {
        substrUbigeo = dataUbigeo[index].substring(0, 4);
        longUbigeo = dataUbigeo[index].length;
        if (substrUbigeo == idProv) {
            if (longUbigeo == 6) {
                selectDistrito.options[selectDistrito.options.length] = new Option(dataUbigeoDesc[index], dataUbigeo[index]);
            }
        }
    }
}